<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">

        <el-form-item>
          <span>订单状态：</span>
          <el-radio-group v-model="selectOptions.status" @change="currentStatusChange">
            <el-radio-button v-for="(item, index) in statusMaps" :label="item.value" name="status" :key="index">{{ item.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>

        <div style="display: inline-block;float: right">
          <el-select v-model="selectOptions.query" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-form-item>
            <el-input :placeholder="selectOptions.query" v-model="selectOptions.keywords" @keyup.enter.native="doSearch"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="doSearch">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="background: #13ce66" v-on:click="_reload">刷新</el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="orders" highlight-current-row v-loading="listLoading"
              style="width: 100%;" element-loading-text="玩命请求中。。。">
      <el-table-column label="ID" width="80" prop="id">
      </el-table-column>
      <el-table-column label="期号" width="100" prop="sn">
      </el-table-column>
      <el-table-column label="订单状态" width="120" prop="order_status" :formatter="buyType">
      </el-table-column>
      <el-table-column prop="nickname" label="用户名" width="130">
      </el-table-column>
      <el-table-column prop="area_name" label="地块" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.area_name }}{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="plant_name" label="菜品" width="100">
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="130">
      </el-table-column>
      <el-table-column prop="goods_amount" label="订单金额(元)" width="120" :formatter="orderAmount">
      </el-table-column>
      <el-table-column prop="fee" label="优惠(元)" width="100" :formatter="orderFee">
      </el-table-column>
      <el-table-column label="支付单号" width="210" prop="pay_id">
      </el-table-column>
      <el-table-column prop="pay_type" label="支付方式" width="100" :formatter="payType">
      </el-table-column>
      <el-table-column label="创建时间" prop="create_time">
      </el-table-column>
      <el-table-column label="注册时间" prop="regtime" :formatter="regTime">
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentPageChange"
        :page-size="30"
        layout="prev, pager, next, jumper"
        :total="total" style="float:right;" :current-page="parseInt(selectOptions.page)">
      </el-pagination>
      <span style="float: right;margin-top: 6px;margin-right: 5px">共 {{total}} 条数据</span>
    </el-col>

  </section>
</template>

<script>
  import util from '../../util'
  import NProgress from 'nprogress'
	import { apiTree } from '../../apiTree'

  export default {
    data() {
      return {
        options: [
          {value: 'sn', label: '期号'},
          {value: 'pay_id', label: '支付单号'},
          {value: 'phone', label: '手机号'},
          {value: 'nickname', label: '昵称'},
          {value: 'plant_name', label: '菜品'},
          {value: 'area_name', label: '区域名'}
        ],
        statusMaps: [
          {value: '', label: '全部'},
          {value: -3, label: '购买失败'},
          {value: -2, label: '超时取消'},
          {value: -1, label: '取消'},
          {value: 1, label: '已支付'},
          {value: 0, label: '待支付'},
          {value: 2, label: '已发货'},
          {value: 3, label: '已完成'},
        ],
        orders: [],
        total: 0,
        listLoading: false,
        selectOptions: {
          page: this.$route.query.page !== undefined ? this.$route.query.page : 1,
          query: this.$route.query.query !== undefined ? this.$route.query.query : '',
          keywords: this.$route.query.keywords !== undefined ? this.$route.query.keywords : '',
          status: this.$route.query.status !== undefined ? this.$route.query.status : ''
        }
      }
    },
    methods: {
      // 购买类型
      buyType(row, column) {
        return {
           '-4': '已支付订单失败',
           '-3': '下单失败',
           '-2': '自动取消',
           '-1': '手动取消',
            '0': '待支付',
            '1': '已支付',
            '2': '已发货',
            '3': '已完成'
        }[row.order_status]
      },
      _reload() {
        this.getOrder()
      },
      // 支付方式
      payType(row, column) {
        if (row.pay_type === '2') {
          return '支付宝'
        } else if (row.pay_type === '3') {
          return '微信'
        } else {
          return '其它'
        }
      },
      // 订单金额
      orderAmount(row, column) {
        return parseInt(row.goods_amount) / 100
      },
      orderFee(row, column) {
        return parseInt(row.fee) / 100
      },
      // 创建时间
      createTime(row, column) {
        return util.formatDate.format(new Date(row.create_time * 1000), 'yyyy/MM/dd hh:mm:ss')
      },
      // 注册时间
      regTime(row) {
        return util.formatDate.format(new Date(row.regtime * 1000), 'yyyy/MM/dd hh:mm:ss')
      },
      // 分页
      currentPageChange(val) {
        this.selectOptions.page = val
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      // 状态搜索
      currentStatusChange() {
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      // 搜索
      doSearch() {
        if (this.selectOptions.query) {
          this.$router.push({path: this.$route.path, query: this.selectOptions})
        }
      },
      // 获取用户列表
      getFieldOrder() {
        let para = this.$route.query
        if (util.isEmptyObject(para)) {
          this.selectOptions = {
            page: 1,
            query: '',
            keywords: '',
            status: ''
          }
        }
        this.listLoading = true
        NProgress.start()
        apiTree('fieldOrder/index', 'order/getFieldOrder', para).then((res) => {
          this.total = res.d.total_num
          this.orders = res.d.data
          this.listLoading = false
          NProgress.done()
        })
      }
    },
    created() {
      this.getFieldOrder()
    },
    watch: {
      $route: 'getFieldOrder'
    }
  }

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .user-avatar
    width 60px
    height 60px
    border-radius 50%
    & img
      width 100%
      height 100%
      border-radius 50%
</style>
